package com.jcfk.eam.controller.mould;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.core.exception.CustomException;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.mould.MouldGroupDetailDTO;
import com.jcfk.eam.domain.dto.mould.MouldGroupRecordDTO;
import com.jcfk.eam.domain.po.main.EamAssetInfo;
import com.jcfk.eam.domain.query.mould.MouldGroupRecordQuery;
import com.jcfk.eam.service.main.EamAssetInfoService;
import com.jcfk.eam.service.mould.MouldGroupRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 模具组合记录
 *
 * @author yrl
 * @email
 * @date 2024-11-11
 */
@Api(tags = {"模具组合记录管理"})
@RestController
@RequestMapping("modgrouprecord")
public class MouldGroupRecordController {
    @Autowired
    private MouldGroupRecordService modGroupRecordService;

    @Resource
    private EamAssetInfoService eamAssetInfoService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<MouldGroupRecordQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询模具组合记录")
    @PostMapping("/queryByPage")
    public RetPage<MouldGroupRecordDTO> queryByPage(@RequestBody ReqPageParam<MouldGroupRecordQuery> requestParam) {
        // 设置默认排序
        if (requestParam.getSort().getOrderItem() == null) {
            ReqSortParam<MouldGroupRecordQuery> sortParam = new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<MouldGroupRecordDTO> page = new DataPage<MouldGroupRecordDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                MouldGroupRecordDTO.class);
        page = modGroupRecordService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "MouldGroupRecordQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取模具组合记录列表", notes = "获取模具组合记录列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<MouldGroupDetailDTO>> getDetails(@RequestBody MouldGroupRecordQuery query) {

        List<MouldGroupDetailDTO> list = modGroupRecordService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "MouldGroupRecordQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取模具组合记录列表", notes = "获取模具组合记录列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<MouldGroupRecordDTO>> getList(@RequestBody MouldGroupRecordQuery query) {

        List<MouldGroupRecordDTO> list = modGroupRecordService.getList(query);

        return RetResult.ok(list);
    }


    /**
     * 模具组合记录
     * 根据id 查询详情
     */
    @ApiOperation("查询模具组合记录")
    @GetMapping("/get/{id}")
    public RetResult<MouldGroupRecordDTO> get(@PathVariable("id") String id) {
        MouldGroupRecordDTO item = modGroupRecordService.get(id);
        return RetResult.ok(item);
    }


    /**
     * 批量删除模具组合记录
     */
    @ApiOperation("批量删除模具组合记录")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "模具组合记录管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody MouldGroupRecordDTO modGroupRecord) {
        return RetResult.ok(modGroupRecordService.logicDeleteList(modGroupRecord));
    }


    /**
     * 删除模具组合记录
     */
    @ApiOperation("删除模具组合记录")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "模具组合记录管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        modGroupRecordService.deleteItem(id);
        return RetResult.ok();
    }

//-----------------------------------------------------------PDA-------------------------------------------------------------------------

    /**
     * 通过bom编码查询模具组合记录
     *
     * @param bomCode bom编码
     */
    @ApiOperation("通过bom编码查询模具组合记录")
    @GetMapping("/getListByBomCode")
    public RetResult<?> getListByBomCode(String bomCode) {
        Assert.notEmpty(bomCode, "BOM编码不能为空");
        return RetResult.ok(modGroupRecordService.getListByBomCode(bomCode));
    }

    /**
     * 查询模具组装列表
     * 组装过的要显示组装的信息
     *
     * @param mouldCode 模具编码
     * @param bomCode   bom编码
     * @return 模具组装列表
     */
    @ApiOperation("查询模具组装列表")
    @GetMapping("/getListByMouldCodeAndBomCode")
    public RetResult<?> getListByMouldCodeAndBomCode(String mouldCode, String bomCode) {
        Assert.notEmpty(bomCode, "BOM编码不能为空");
        return RetResult.ok(modGroupRecordService.getListByMouldCodeAndBomCode(mouldCode, bomCode));
    }


    /**
     * 校验扫码模具编码是否可用
     *
     * @param mouldCode     模具编码
     * @param bomCode       bom编码
     * @param scanMouldCode 要进行组装的模具编码
     */
    @ApiOperation("查询模具组装列表")
    @GetMapping("/validScanMould")
    public RetResult<?> validScanMould(String mouldCode, String bomCode, String scanMouldCode) {
        Assert.notEmpty(bomCode, "BOM编码不能为空");
        Assert.notEmpty(scanMouldCode, "要进行组装的模具编码不能为空");
        modGroupRecordService.validScanMould(mouldCode, bomCode, scanMouldCode);
        return RetResult.ok();
    }


    /**
     * 添加/更新管理模具组合记录
     */
    @ApiOperation("保存模具组合记录")
    @PostMapping("/save")
    @RequestLogHeader(modular = "模具组合记录管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid MouldGroupRecordDTO modGroupRecord) {
        // 数据校验
        RetResult ret = modGroupRecordService.isValid(modGroupRecord);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(modGroupRecord.getId())) {
            // 新增
            modGroupRecordService.insert(modGroupRecord);
        } else {
            // 更新
            modGroupRecordService.update(modGroupRecord);
        }
        return RetResult.ok();

    }


    @ApiOperation("通过子表查询模具组合主表数据")
    @GetMapping("/getGroupRecordBySubMould")
    public RetResult<?> getGroupRecordBySubMould(String mouldCode) {
        // 获取模具台账信息
        QueryWrapper<EamAssetInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamAssetInfo::getAssetCode, mouldCode);
        EamAssetInfo eamAssetInfo = eamAssetInfoService.getOne(queryWrapper);
        // if (eamAssetInfo.getStorageStatus() == 3) {
        //     throw new CustomException("该模具库存状态为设备，无需再操作!");
        // }
        if (eamAssetInfo.getAssetStatus() == 2) {
            throw new CustomException("该模具状态为使用中，无需再操作!");
        }
        return RetResult.ok(modGroupRecordService.getGroupRecordBySubMould(mouldCode));
    }


    /**
     * 打散
     */
    @ApiOperation("打散")
    @PostMapping("/scatter/{id}")
    @RequestLogHeader(modular = "打散", businessType = "删除")
    public RetResult<?> scatter(@PathVariable("id") String id) {
        modGroupRecordService.scatter(id);
        return RetResult.ok();
    }

}
